package com.zt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.Result;
import com.zt.pojo.Roles;
import com.zt.pojo.Users;
import com.zt.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;

@RestController
@RequestMapping("/api")
public class RoleController {
    @Autowired
    private RolesService rolesService;

    /**
     * 添加角色
     * @param roles
     * @return
     */
    @PostMapping("/roles")
    public Result addRole(@RequestBody Roles roles){
        if (rolesService.getOne(new QueryWrapper<Roles>().lambda().eq(Roles::getName,roles.getName()))!=null){
            return Result.ERROR("该角色已存在");
        }
        boolean save = rolesService.save(roles);
        return save?Result.OK():Result.ERROR();
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/roles")
    public Result roleList(){
        return Result.OK(rolesService.list());
    }

    /**
     * 更新角色信息
     * @param roles
     * @param id
     * @return
     */
    @PutMapping("/roles/{id}")
    public Result updateRole(@RequestBody Roles roles, @PathVariable Integer id){
        Roles byId = rolesService.getById(id);
        boolean update=false;
        if (byId!=null){
            update=rolesService.updateById(roles);
        }
        return update?Result.OK():Result.ERROR();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/roles/{id}")
    public Result deletd(@PathVariable Integer id){
        Roles byId = rolesService.getById(id);
        boolean delete=false;
        if (byId!=null){
            delete=rolesService.removeById(byId);
        }
        return delete?Result.OK():Result.ERROR();
    }

}
